Ext.define('AmpleHarvest.views.article.cTranslationButton',
{
	extend				: 'Ext.button.Button',
	alias				: 'widget.ampleharvest-views-article-ctranslatebutton',

	ahCaller			: null,

	listeners			: {
		
		handleTranslateResponse : function(response) {
			
			if(!response.success || response.metaData.hasErrors) {
				this.showErrorWindow(response);
			} else {
				this.showSuccessWindow(response);
			}
			
			this.disableTranslateButton(false);
		}
	},
	
	showErrorWindow : function(response) {
		var jsonDisplayHelper = Ext.create('AmpleHarvest.util.nestedJsonUi', 
				{fieldsToIgnore : ['ajaxProperties']});
		
		this.showResultWindow('Translation Error', 
		{
			title:'Error Response',
			region:'center',
        	autoScroll:true,
        	items: jsonDisplayHelper.processObjectData(response)
        });
	},
	
	showSuccessWindow : function(response) {
		var jsonDisplayHelper = Ext.create('AmpleHarvest.util.nestedJsonUi', 
				{fieldsToIgnore : ['translation','ajaxProperties']});
		
		var decoder = Ext.create('AmpleHarvest.util.encodingHelper');
		
		this.showResultWindow('Translation',
			[{
					autoScroll: true,
					region:'center',
		            html: decoder.base64_decode(response.translation)
		        }, {
		        	region:'south',
		        	title:'Details',
		        	flex:.5,
		        	collapsible:true,
		        	autoScroll:true,
		        	items: jsonDisplayHelper.processObjectData(response)
	        }],
	        response.metaData.classification);
	},
	
	createBanner : function(dock) {
		return Ext.create("AmpleHarvest.layout.cClassBanner",
				{
					dock			: dock,
					height			: 35,
					classification	: "UNCLASSIFIED" //will be updated
				}
		);
	},
	
	showResultWindow : function(title, items, classification) {
		var m_obj_ClassBannerTop = this.createBanner('top');
		var m_obj_ClassBannerBottom = this.createBanner('bottom');
		
		if(classification) {
			m_obj_ClassBannerTop.change_Classification(classification);
			m_obj_ClassBannerBottom.change_Classification(classification);
		}
		
		var container = this.getParentContainer();
		
		Ext.create('Ext.window.Window', {
			title: title,
			width: container.getWidth()*3/4,
			height: container.getHeight()*3/4,
			cls: 'html',
			modal: true,
			layout:'border',
			dockedItems: [m_obj_ClassBannerTop, m_obj_ClassBannerBottom],
			defaults: {
				split: true
			},
			items: items
			
		}).show();
	},
	
	getParentContainer : function() {
		return this.ahCaller.ownerCt.ownerCt.ownerCt;
	},

	initComponent : function() {
		this.text = 'Translate';
		this.handler = this.translate;
		this.scope = this;
		
		this.m_obj_util = Ext.create('AmpleHarvest.util.utility');
		this.m_obj_util.AHcaller = this;
		this.m_obj_util.listenerName = 'handleTranslateResponse';
      	
		this.callParent();
	},

	//TODO verifyData? currently handled in actual response
	
	disableTranslateButton : function(disable) {
		this.disabled = disable;
		if(disable) {
			this.setTooltip('Translating...');
		} else {
			this.setTooltip('');
		}
	},
	
	
	translate : function() {
		this.disableTranslateButton(true);
		
		var contentObject = this.ahCaller.down("ampleharvest-objects-content-ccontent");
		
		var params = {
				inputText : contentObject.translateContent, 
				userEmail : obj_config.user.email1, 
				classification : contentObject.classification,
				outputLanguage: 'English',
				outputFormat: 'Text',
				
				clientName : obj_config.C_STR_APPLICATION,
				clientVersion: obj_config.C_STR_VERSION
		};
		
		var jsonPostData = {
				translateRequest : params
		};
		
		var headers = {Accept : 'application/json'};
		
		this.m_obj_util.getData('/Translate',
//				params,null, //for low-side testing
				null,jsonPostData, //actual data
				'POST',{},headers);

	},
}
);